import time

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

# 1.先创建浏览器驱动对象
driver = webdriver.Chrome()
# 2.通过get方法打开页面
print(driver.service.path)  # 本地驱动的缓存路径
driver.get("http://localhost:82/")
driver.maximize_window()#浏览器最大化
element=WebDriverWait(driver,5)
# driver.find_elements  把相同的页面元素以列表的方式返回   一般用不到
driver.find_element(By.CSS_SELECTOR,value=".red").click()
driver.find_element(By.CSS_SELECTOR, "#username").send_keys("13800138006")
driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
driver.find_element(By.CSS_SELECTOR,"#verify_code").send_keys("8888")
driver.find_element(by=By.PARTIAL_LINK_TEXT,value="登    录").click()
time.sleep(3)
driver.find_element(By.CSS_SELECTOR,"a[href='/index.php/Home/Index/index.html']").click()
time.sleep(3)
driver.find_element(By.CSS_SELECTOR,"#q").send_keys("小米")
driver.find_element(By.CSS_SELECTOR,"button[type='submit']").click()
time.sleep(3)
driver.find_element(By.CSS_SELECTOR,".lazy-list[data-original='/public/upload/goods/thumb/5/goods_thumb_5_0_236_236.png']").click()
for add in range(11):
    driver.find_element(By.CSS_SELECTOR,".add").click()
time.sleep(1)
ele=driver.find_element(By.CSS_SELECTOR,"#number")
ele.send_keys(Keys.CONTROL,'a')
ele.clear()
time.sleep(1)
ele=driver.find_element(By.CSS_SELECTOR,"#number")
ele.send_keys(Keys.CONTROL,'a')
ele.send_keys("1")
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"#buy_now").click()
time.sleep(1)
#创建弹出框对象
alert=driver.switch_to.alert
time.sleep(1)
alert.accept()#点击确定
driver.find_element(By.CSS_SELECTOR,"#addNewAddress").click()
ele=driver.find_element(By.CSS_SELECTOR,"#addNewAddress")
ele1=driver.find_element(By.CSS_SELECTOR,"input[name='consignee'][type='text']")
#向输入框自动输入当前时间
ele1.send_keys(time.strftime("cus%H%M%S"))
ele2=driver.find_element(By.CSS_SELECTOR,"input[placeholder='固话号码格式:xx-xx(例如: 0755-86140485)']")
ele2.send_keys("15560015310")
Select(driver.find_element(By.CSS_SELECTOR,"#province")).select_by_visible_text("河南省")
time.sleep(1)
Select(driver.find_element(By.CSS_SELECTOR,"#city")).select_by_visible_text("周口市")
time.sleep(1)
Select(driver.find_element(By.CSS_SELECTOR,"#district")).select_by_visible_text("川汇区")
time.sleep(1)
Select(driver.find_element(By.CSS_SELECTOR,"#twon")).select_by_visible_text("搬口")
time.sleep(1)
select4=driver.find_element(By.CSS_SELECTOR,"input[placeholder='详细地址']")
select4.send_keys("河南省周口市搬口街道青网科技园二号楼艾腾教育")
time.sleep(1)
select5=driver.find_element(By.CSS_SELECTOR,"input[name='zipcode']")
select5.send_keys("476300")
time.sleep(1)
select6=driver.find_element(By.CSS_SELECTOR,"#address_submit")
select6.click()
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"div[class='addr-switch'] span").click()
time.sleep(1)
#获取地址类表的长度
select8=driver.find_element(By.CSS_SELECTOR,"li:nth-child(5) div:nth-child(1) span:nth-child(1)").click()
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"div[class='addr-switch switch-on'] span").click()
time.sleep(1)
select10=driver.find_element(By.CSS_SELECTOR,"textarea[placeholder='最多输入50个字']")
select10.clear()
select10.send_keys("请尽快发货")
time.sleep(1)
js="window.scrollTo(0, 1100)"
driver.execute_script(js)
#获取应付金额
pay_money=driver.find_element(By.CSS_SELECTOR,"#payables").text
pay_money=pay_money.split("￥")[1]
pay_money=float(pay_money)

# pay_money=pay_money.split("￥")[1]
print(pay_money)
#获取当前金额
#点击
# driver.find_element(By.CSS_SELECTOR,"#user_money_checkbox").click()
#获取实际金额
pay_money_1=driver.find_element(By.CSS_SELECTOR,"body > div:nth-child(13) > div:nth-child(1) > div:nth-child(2) > div:nth-child(3) > p:nth-child(2) > label:nth-child(1) > span:nth-child(3)").text
pay_money_1=float(pay_money_1)
print(pay_money_1)
if pay_money<=pay_money_1:
    print("余额充足")
    driver.find_element(By.CSS_SELECTOR,"#user_money_checkbox").click()
    time.sleep(1)
    driver_1=driver.find_element(By.CSS_SELECTOR,"#user_money")
    driver_1.click()
    driver_1.send_keys(f"{pay_money}")
else:
    print("余额不足")
driver_2=driver.find_element(By.CSS_SELECTOR,"#pay_pwd")
driver_2.click()
driver_2.send_keys("123456")
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"#submit_order").click()
time.sleep(10)
if pay_money<=pay_money_1:
    ele_1=driver.find_element(By.CSS_SELECTOR,"div[class='home-main'] span:nth-child(2)")
    print(ele_1.text)
else:
    ele=driver.find_element(By.CSS_SELECTOR,".succ-p")
    print(ele.split("：")[1].split("    |")[0].strip())
#只获取订单编号
# ele=ele.split("：")[1].split("    |")[0].strip()
# print(ele)
# print(ele.split("：")[1].split("    |")[0].strip())
# 3.操作页面的元素  比如输入，点击，双击，鼠标的移动等等
time.sleep(30)
# 4.关闭浏览器驱动对象
driver.quit()